import formatDate from '../../../utils/formatdate'

Page({
  data: {
    currentDate: new Date().getTime(),
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    houseInfo: {},
    name: "",
    gender: 1,
    mobile: "",
    visitDate: ""
  },
  onLoad() {
    this.getRomm()
  },
  async getRomm() {
    let res = await wx.http.get('/house')
    this.setData({
      houseList: res
    })
  },
  HouseSelect(event) {
    this.setData({
      houseInfo: event.detail
    })
  },
  selectDate(event) {
    this.setData({
      visitDate: formatDate(new Date(event.detail), 'yyyy-MM-dd'),
      dateLayerVisible: false
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goPassport() {

    let { houseInfo,name, gender, mobile, visitDate } = this.data

    const toast = (title) => {
      wx.showToast({
        title,
        icon: 'none'
      })
    }

    if (!houseInfo.hasOwnProperty('id') || houseInfo.id == '') {
      return toast('房屋信息不能为空')
    }

    if (name.trim() === "") {
      return toast('姓名不能为空')
    }

    if (!/^1[0-9]{10}$/.test(mobile)) {
      return toast('请填写有效的手机号')
    }

    if (visitDate === "") {
      return toast('请选择访问日期')
    }

    let params = {
      houseId: houseInfo.id,
      name,
      gender,
      mobile,
      visitDate
    }

    await wx.http.post('/visitor', params)

    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index',
    })
  },
})
